import java.util.Stack;

public class IsValid {
    public static void main(String[] args) {
        System.out.println(isValid("([)"));
    }

    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '[' || c == '{'){
                stack.push(c);
            }else{
                if (stack.isEmpty()){
                    return false;
                }
                switch (c){
                    case ')':
                        if (stack.peek() == '('){
                            stack.pop();
                        }else{
                            return false;
                        }
                        break;
                    case ']':
                        if (stack.peek() == '['){
                            stack.pop();
                        }else{
                            return false;
                        }
                        break;
                    case '}':
                        if (stack.peek() == '{'){
                            stack.pop();
                        }else{
                            return false;
                        }
                        break;
                    default:return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
